package project.android;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class GroupListViewAdapter extends BaseAdapter{
	
	final Context context;
    static ArrayList<String> groups;
    private String[] guidList;
    

    public GroupListViewAdapter(Context context, ArrayList<String> data, String[] guid) {
        this.context = context;
        groups = data;
        this.guidList = guid;
    }

    public int getCount() {
        return groups.size();
    }

    public Object getItem(int position) {
        return groups.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        
        final int currentPosition = position;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_row_view, null);
        }
        
        TextView groupname = (TextView) convertView.findViewById(R.id.groupName);
        groupname.setText(groups.get(position));

        // Set the onClick Listener on view users button
        Button viewUsers = (Button) convertView.findViewById(R.id.viewUsersButton);
        viewUsers.setFocusableInTouchMode(false);
        viewUsers.setFocusable(false);
        viewUsers.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(context, GroupUserPage.class);
            	int j;
            	for(j = 0; j < groups.size();j++)
            		if(groups.get(j) == getItem(currentPosition)) break;
            	i.putExtra("GROUPGUID", guidList[j]);
            	context.startActivity(i);
            }
          });
        
        // Set the onClick Listener on view tasks button
        Button viewTasks = (Button) convertView.findViewById(R.id.viewTasksButton);
        viewTasks.setFocusableInTouchMode(false);
        viewTasks.setFocusable(false);
        viewTasks.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(context, GroupTaskPage.class);
            	int j;
            	for(j = 0; j < groups.size();j++)
            		if(groups.get(j) == getItem(currentPosition)) break;
            	i.putExtra("GROUPGUID", guidList[j]);
            	context.startActivity(i);
            }
          });
        
        // Set the onClick Listener on leave groups button
        Button leaveGroup = (Button) convertView.findViewById(R.id.leaveGroupButton);
        leaveGroup.setFocusableInTouchMode(false);
        leaveGroup.setFocusable(false);
        leaveGroup.setOnClickListener(new View.OnClickListener() {
        	
   
            public void onClick(View v) {
            	
            	int j;
            	for(j = 0; j < groups.size();j++)
            		if(groups.get(j) == getItem(currentPosition)) break;
            	 try{
                 	Synchronized.syncGroups(new DatabaseConnection(context),guidList[j],"groups/leavegroupandroid");
                 }
                 catch(Exception e){
                 	new AlertDialog.Builder(context)
                 	.setTitle("Server Connection Error")
                 	.setMessage(e.getStackTrace().toString())
         	        .setNeutralButton(android.R.string.cancel,
         	            new DialogInterface.OnClickListener() {
         		           public void onClick(DialogInterface dialog, int whichButton){
         		        	  
         		           }
         	        })
         	        .show();
                 }
            	
            }
        });
       
        return convertView;
    }

}
